<?php

namespace App\Http\Controllers;

use App\Handlers\WechatConfigHandler;
use App\Http\Controllers\Api\EfutureAccessTokenController;
use App\Jobs\PushCardMessage;
use App\Models\Merchant;
use App\Models\WxuserMcard;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Log;

class TestController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(EfutureAccessTokenController $accessTokenController)
    {

        /*$merchant = Merchant::findMerchantById(465);

        $wx = new WechatConfigHandler();

        $buttons = [
            [
                "type" => "view",
                "name" => "我的服务",
                "url"  => "http://jiebai.visastandards.com/my-service"
            ],
            [

                "type" => "view",
                "name" => "个人中心",
                "url"  => "http://jiebai.visastandards.com/user/card-list"

            ],
            [
                "name"       => "活动",
                "sub_button" => [
                    [
                        "type" => "click",
                        "name" => "营销活动",
                        "key"  => "V1001_TODAY_ACTIVE"
                    ],
                ],
            ],
        ];
        $res = $wx->app($merchant)->menu->create($buttons);
        dd($res);
*/

        $wxuser_mcard = WxuserMcard::find(18119);
        $old_cards = $wxuser_mcard->jbUserMcard()->select('cdmno')->get()->map(function($item){
            return $item->getCardTypeByCdmno($item->cdmno);
        })->collapse()->toArray();
dump($old_cards);

$new_cards['p_oldcardno'] = $old_cards['p_oldcardno']??'';
$new_cards['p_hdcardno'] = $old_cards['p_hdcardno']??'';
$new_cards['p_lmcardno'] = $old_cards['p_lmcardno']??'';
dd($new_cards);
        PushCardMessage::dispatch($wxuser_mcard);

        dd(time());
        $token_data = $accessTokenController->accessToken()->getOriginalContent();

        if(isset($token_data['access_token'])){

            $now_time = time();

            $url = 'http://122.224.86.66:8001/datahub/cmd/9011?appid=test&ACCESS_TOKEN='.$token_data['access_token'];

            $post_data = [
                'timestamp'=>$now_time,

                'params'=>['p_mode'=>2, 'p_cond'=>15623557683],

                'sign'=>$this->getSign([$now_time,'pamtest'])
            ];

            $form_data = ['body'=>json_encode($post_data)];

            $client = new Client();
            $result = $client->request('POST',$url,$form_data);
            return $result;
        }else{
            return [];
        }


    }

    public function getSign(array $sign_data):string
    {
        rsort($sign_data);

        return sha1(implode($sign_data));
    }


}
